<?php

namespace App\Http\Controllers;

use App\Model\News;
use Illuminate\Http\Request;
use App\Utils\Page;
use Illuminate\Support\Facades\Cache;

class NewsController extends Controller
{
    protected static $class_info = ['news'=>1,'rjjc'=>3];

    protected function Index(Request $request){
        $classname = $request->classname;
        if(array_key_exists($classname,self::$class_info)){
            $classid = self::$class_info[$classname];
        }else{
            abort(404);
        }
        $pages = $request->page;
        $request->merge(['page'=>$pages]); //分页参数
        $new_list = News::where('classid',$classid)->select('title','titlepic','smalltext','titleurl','id')->paginate(12);
        if($new_list->lastPage() < $pages) {
            abort(404);
        }
    //    dd($new_list);
        $show_page = new Page($new_list->total(), $new_list->perPage(), $classname, $pages, ''); //分页信息
        return view('News.index',compact('new_list','show_page','classname'));
    }
    protected function detail(Request $request){
        preg_match('/(^\d+)\.html/i', $request->id, $newsid);
        $newsid = (int)$newsid[1];
        $softContent = Cache::remember(__FUNCTION__ . '_softContent_' . $newsid, env('SHADUSOFT_CACHE_LIFETIME'), function () use ($newsid) {
            $info = News::leftjoin('ecms_news_data_1 as d1','d1.id','=','ecms_news.id')
                ->where('ecms_news.id',$newsid)
                ->select('ecms_news.id','ecms_news.title','ecms_news.newstime','d1.newstext')
                ->first();
            return $info;
        });
        if(empty($softContent))
            abort(404);
        return view('News.detail',compact('softContent'));
    }
}
